package dk.itu.mmad.travelapp5;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class Main extends Activity{
	private String checkin = "";
	private String checkout = "";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	@Override
	protected void onStart(){
		super.onStart();
		final Button b1 = (Button) findViewById(R.id.b1);
		final Button b2= (Button) findViewById(R.id.b2);
		if(!checkin.equals("")){
			b2.setEnabled(false);
		}else{
			b2.setEnabled(true);
			b1.setEnabled(false);
			}
		b1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText ed1 = (EditText) findViewById(R.id.text1);
				checkin = (String) ed1.getText().toString();
				if(checkin.equals("")){
					Toast.makeText(getApplicationContext(), "You have to type your origin first.", Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(getApplicationContext(), "Origin: " + checkin, Toast.LENGTH_SHORT).show();
					b2.setEnabled(true);
					b1.setEnabled(false);
				}
			}
		});
		b2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText ed2 = (EditText) findViewById(R.id.text2);
				checkout = (String) ed2.getText().toString();
				if(checkout.equals("")){
					Toast.makeText(getApplicationContext(), "Enter your end stop", Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(getApplicationContext(), "Destination: " + checkout, Toast.LENGTH_SHORT).show();
					EditText ed1 = (EditText) findViewById(R.id.text1);
					ed1.setText("");
					ed2.setText("");
					b2.setEnabled(false);
					b1.setEnabled(true);
					//Toast.makeText(getApplicationContext(), "Your travel is over", Toast.LENGTH_SHORT).show();
				}
			}
		});
		if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
		ImageView web = (ImageView) findViewById(R.id.image);
		web.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Main.this, OnWeb.class);
				startActivity(i);
				
			}
		});}
	}
	public void openList(View w){
		int code;
		if(w.equals(findViewById(R.id.bs1))){
			code =1;
		}else{code = 2;}
		Intent i = new Intent(this, Stations.class);
		startActivityForResult(i, code);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		String station_1;
		if(resultCode == RESULT_OK){
			station_1 = (String) data.getStringExtra("STATION");
			putText(requestCode, station_1);
		}
	}

	private void putText(int rq, String station){
		EditText ed = null; 
		if(rq == 1){
			ed = (EditText) findViewById(R.id.text1);
		}else{
			ed = (EditText) findViewById(R.id.text2);
		}
		ed.setText(station);
	}
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putString("s1", checkin);
		outState.putString("s2", checkout);
		EditText ed1 = (EditText) findViewById(R.id.text1);
		outState.putString("e1", ed1.getText().toString());
		EditText ed2 = (EditText) findViewById(R.id.text2);
		outState.putString("e2", ed2.getText().toString());
	}
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		checkin = savedInstanceState.getString("s1");
		checkout = savedInstanceState.getString("s2");
		putText(1, savedInstanceState.getString("e1"));
		putText(2, savedInstanceState.getString("e2"));
	}

	MenuItem item;
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		item = menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "Receipt");
		item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		featureId = Menu.FIRST;
		item = this.item;
		Toast.makeText(getApplicationContext(), "You have traveled from " + checkin + " to " + checkout, Toast.LENGTH_LONG).show();
		return super.onMenuItemSelected(featureId, item);
	}
}
